<?php
class banner_model extends Model
{
	function banner_model()
	{
	  parent::Model();
	  $this->load->helper('img_helper');  
	}
	
	/*****banner**********/
	function getAllBanner($num, $offset)
	{
		$this->db->where('status', 1);
		$this->db->select('banner.*');
		$this->db->order_by('lastedit', 'desc');
		$query = $this->db->get('banner',$num,$offset);
		return $query->result();
	}
  
	function getNumBanner()
	{
	  $this->db->select('banner.*');
	  $this->db->where('status', 1);
	  $query = $this->db->get('banner');
	  return $query->num_rows();
	}
	
	function get_info_banner($id)
	{
	  $this->db->where('bn_id',$id);
	  $query = $this->db->get('banner');
	  return $query->row();
	}
	
	function savebanner()
	{
		$id = (int)$this->uri->segment(3);
		$data = array(
			'bn_name' 	=> $this->input->post('title'),
			'bn_images' => $this->input->post('name'),
			'position' 	=> 1, //$this->input->post('url'),
			'lastedit' 	=> time(),
			'status' 	=> (int)($this->input->post('status'))
	  );

	  if($id !=0)
	  {
		  // Update banners
		  $this->db->where('bn_id',$id);
		  if($this->db->update('banner',$data))
		  {
			  return true;
		  }
		  else
		  {
			  return false;
		  }
	  }
	  else
	  {
		  // Add new banners
		  if($this->db->insert('banner',$data))
		  {
			  return true;
		  }
		  else
		  {
			  return false;
		  }
	  }
	}
	
	function del($id)
	{
		$data = array(
			'lastedit' 	=> time(),
			'status' 	=> -1
		);
		
		$this->db->where('bn_id',$id);
		//$query = $this->db->update('banner');trit($id);
		if($this->db->update('banner',$data))
		{
			return true;
		}
		else
		{
			return false;
		}          
	}   
	
	/*function delbanner($id)
	{
		$this->db->where_in('id',$id);
		$query = $this->db->delete('banner');
		if($query)
		{
			return true;
		}
		else
		{
			return false;
		}          
	}*/   
	
	/*function get_search($catid, $level, $num=20, $offset=0)
	{
		if(!empty($catid)) $this->db->where('catid', $catid);
		if(!empty($level)) $this->db->where('pro_level', $level);
		
		$this->db->select('banner.*');
		$query = $this->db->get('banner',$num,$offset);
		return $query->result();
	}
	
	function get_total_search($cid)
	{
		if(!empty($catid)) $this->db->where('catid', $catid);
		if(!empty($level)) $this->db->where('pro_level', $level);
		
		$this->db->select('banner.*');
		$query = $this->db->get('banner');
		return $query->num_rows();
	}*/
	
	
	// Tong so san pham
	/*function get_total_banner($IsActive)
	{
	  $this->db->select('id');
	  $this->db->where('IsActive',$IsActive);
	  return $this->db->get('banner')->num_rows();
	}*/

}
?>
